import axios from 'axios'

const service = axios.create({
  baseURL: process.env.NODE_ENV === 'production' ? '/api' : 'http://localhost:4000/api',
  timeout: 5000,
  withCredentials: true
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    if (response.data.code && response.data.code !== 200) {
      return Promise.reject(response.data)
    }
    return response.data
  },
  error => {
    if (error.response && error.response.status === 401) {
      // 处理未授权情况
    }
    return Promise.reject(error)
  }
)

// 定义10个接口请求函数
export const fetchUserInfo = () => service.get('/user-info')        // 用户信息
export const fetchCompanyInfo = () => service.get('/company-info') // 公司信息
export const fetchRevenue = () => service.get('/revenue')          // 营业额
export const fetchOrderInfo = () => service.get('/order-info')     // 订单信息
export const fetchImageInfo = () => service.post('/image-info')    // 图片信息
export const fetchLocationInfo = () => service.post('/location-info') // 位置信息
export const fetchDataInfo = () => service.post('/data-info')      // 数据信息
export const fetchStatistics = () => service.post('/statistics')   // 统计信息
export const fetchOptionInfo = () => service.post('/option-info')  // 选项信息
export const fetchOtherInfo = () => service.post('/other-info')    // 其他信息